package org.classg.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.classg.servicebase.exception.MyException;
import org.classg.statistics.client.UcenterClient;
import org.classg.statistics.entity.StatisticsDaily;
import org.classg.statistics.mapper.StatisticsDailyMapper;
import org.classg.statistics.service.StatisticsDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-19
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    // 根据天数统计人数，生成统计数据
    @Override
    public void insertStaByDay(String day) {
        // 删除相同日期的记录
        baseMapper.delete(new LambdaQueryWrapper<StatisticsDaily>().eq(StatisticsDaily::getDateCalculated, day));

        // 获取统计信息
        Integer registerNum = (Integer) ucenterClient.queryRegisterCountByDay(day).getData().get("countPeople");
        Integer loginNum = RandomUtils.nextInt(100, 200);
        Integer videoViewNum = RandomUtils.nextInt(100, 200);
        Integer courseNum = RandomUtils.nextInt(100, 200);

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(registerNum);
        statisticsDaily.setLoginNum(loginNum);
        statisticsDaily.setVideoViewNum(videoViewNum);
        statisticsDaily.setCourseNum(courseNum);
        statisticsDaily.setDateCalculated(day);

        // 添加数据
        baseMapper.insert(statisticsDaily);

    }

    // 图表显示，返回2部分数据，日期json数组和数量json数组
    @Override
    public Map<String, Object> queryStaToJsonArray(String type, String begin, String end) {
        if (StringUtils.isEmpty(type) || StringUtils.isEmpty(begin) || StringUtils.isEmpty(end)) {
            throw new MyException(20001, "图表显示条件查询失败！");
        }
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.select(type, "date_calculated");
        wrapper.between("date_calculated", begin, end);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);
        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        for (StatisticsDaily statisticsDaily : statisticsDailies) {
            dateList.add(statisticsDaily.getDateCalculated());
            switch (type) {
                case "login_num" :
                    dataList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num" :
                    dataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num" :
                    dataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num" :
                    dataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
            map.put("dataList", dataList);
            map.put("dateList", dateList);
        }
        return map;
    }
}
